<?php

/**
 * Frog CMS - Content Management Simplified. <http://www.madebyfrog.com>
 * Copyright (C) 2008 Philippe Archambault <philippe.archambault@gmail.com>
 *
 * This file is part of Frog CMS.
 *
 * Frog CMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Frog CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Frog CMS.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Frog CMS has made an exception to the GNU General Public License for plugins.
 * See exception.txt for details and the full text.
 */

define('DEFAULT_ADMIN_USER', 'admin');
define('DEFAULT_EDITOR_USER', 'editor');
define('CORE_ROOT', dirname(__FILE__).'/../frog');

$config_file = '../config.php';

include 'Template.php';

if (file_exists($config_file))
  include $config_file;

$msg = '';
$PDO = false;

// lets install this nice little CMS
if ( ! defined('DEBUG') && isset($_POST['commit']) && (file_exists($config_file) && is_writable($config_file)))
{
    define('INSTALL_SEQUENCE', true);
    $admin_name = DEFAULT_ADMIN_USER;
    $admin_passwd = '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8';
    $admin_passwd_precrypt = 'password';

    $_POST['config']['use_pdo'] = class_exists('PDO');

    // check if the PDO driver we need is installed
    if ($_POST['config']['use_pdo'] and !in_array($_POST['config']['db_driver'], PDO::getAvailableDrivers()))
        $_POST['config']['use_pdo'] = false;

    $config_tmpl = new Template('config.tmpl');
    
    $config_tmpl->assign($_POST['config']);
    $config_content = $config_tmpl->fetch();
    
    file_put_contents($config_file, $config_content);
    $msg .= "<ul><li>Config file successfully written.</li>\n";
    
    include $config_file;

    // setup admin name (default to 'admin')
    if (isset($_POST['config']['admin_username'])) {
        $admin_name = $_POST['config']['admin_username'];
        $admin_name = trim($admin_name);

        $admin_passwd_precrypt = 'demo';
        $admin_passwd = sha1($admin_passwd_precrypt);
    }
    
    // editor name
    $editor_name = $_POST['config']['editor_username'];
    $editor_passwd_precrypt = "demo";
    $editor_passwd = sha1($editor_passwd_precrypt);
    
    if (USE_PDO)
    {
        try {
            $PDO = new PDO(DB_DSN, DB_USER, DB_PASS);
        } catch (PDOException $e) {
            $msg = 'Frog has not been installed properly.<br />The following error has occured: <p><strong>'. $e->getMessage() ."</strong></p>\n";
            file_put_contents($config_file, '');
        }
    }
    else if ($_POST['config']['db_driver'] == 'mysql')
    {
        require_once CORE_ROOT . '/libraries/DoLite.php';
        $PDO = new DoLite(DB_DSN, DB_USER, DB_PASS);
    }
    else
    {
        $msg = "Frog has not been installed properly.<br />You need PDO and SQLite 3 drive to use SQLite 3.<br />\n";
    }
    
    if ($PDO)
    {
        $msg .= '<li>Database connection successfull.</li>';
        
        include 'schema_'.$_POST['config']['db_driver'].'.php';
        include 'sql_data.php';
        
        $msg .= '<li>Tables loaded successfully</li></ul><p>You can login as administrator with: <br /><br /><strong>login</strong>: '.$admin_name.'<br /><strong>password</strong>: '.$admin_passwd_precrypt.'<br /><br /><p>Or login as editor with: <br /><br /><strong>login</strong>: '.$editor_name.'<br /><strong>password</strong>: '.$editor_passwd_precrypt.'<br /><br />

        <strong>at</strong>: <a href="../admin/">login page</a></p>

        <p>Please be aware: the passwords are generated by Polliwog, please use it to login to Polliwog and <strong>change the passwords</strong>!</p>';
    }
    else
    {
    	$error = 'Unable to connect to the database! Tables are not loaded!';
    	// delete old config
    	unlink($config_file);
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Polliwog CMS - Install</title>
<link href="../admin/stylesheets/admin.css" media="screen" rel="Stylesheet" type="text/css" />
<script type="text/javascript" charset="utf-8" src="../admin/javascripts/prototype.js"></script>
<script type="text/javascript" charset="utf-8" src="../admin/javascripts/effects.js"></script>
</head>
<body id="installation">
<div id="header">
  <img src="../admin/images/polliwog.png" id="logo" style="height:80px;" />
  <div id="site-title">Polliwog CMS</div>
</div>
<div id="main">
  <div id="content-wrapper">
      <div id="content">
              <!-- Content -->
        <h1>Polliwog Installation</h1>

<p style="color: red">
<?php if ( ! file_exists($config_file)) {
  if (!touch($config_file))
  { ?>
    <strong>Error</strong>: config.php doesn't exist and cannot be created<br />
<?php }
} else if ( !is_writable($config_file)) { ?>
  <strong>Error</strong>: config.php must be writable<br />
<?php } ?>
<?php if ( ! is_writable('../public/')): ?>
  <strong>Error</strong>: public/ folder must be writable<br />
<?php endif; ?>
</p>

<?php if ( ! defined('DEBUG')): ?>
<form action="index.php" method="post">
  <table class="fieldset" cellpadding="0" cellspacing="0" border="0" summary="Install parameters">
    <thead style="display:none;">
      <tr>
        <th>Label</th>
        <th>Field</th>
        <th>Help</th>
      </tr>
    </thead>
    <tbody>
       <tr>
         <td colspan="3"><h3>Database information</h3></td>
       </tr>
       <tr>
         <td class="label"><label for="config_db_driver">Database driver</label></td>
         <td class="field">
           <select id="config_db_driver" name="config[db_driver]" onchange="db_driver_change(this[this.selectedIndex].value);">
             <option value="mysql">MySQL</option>
             <option value="sqlite">SQLite 3</option>
           </select>
           <script type="text/javascript" language="javascript">
             function db_driver_change(driver)
             {
               Element.toggle('row-db-host');
               Element.toggle('row-db-port');
               Element.toggle('row-db-user');
               Element.toggle('row-db-pass');
               Element.toggle('row-table-prefix');

               if (driver == 'sqlite')
               {
                 $('help-db-name').innerHTML = 'Required. Enter the <strong>absolute</strong> path to the database file.';
               }
               else if (driver == 'mysql')
               {
                 $('help-db-name').innerHTML = 'Required. You have to create a database manually and enter its name here.';
               }
             }
           </script>
         </td>
         <td class="help">Required. PDO support and the SQLite 3 plugin are required to use SQLite 3.</td>
       </tr>
       <tr id="row-db-host">
         <td class="label"><label for="config_db_host">Database server</label></td>
         <td class="field"><input class="textbox" id="config_db_host" maxlength="100" name="config[db_host]" size="40" type="text" value="localhost" /></td>
         <td class="help">Required.</td>
       </tr>
       <tr id="row-db-port">
         <td class="label"><label for="config_db_port">Port</label></td>
         <td class="field"><input class="textbox" id="config_db_port" maxlength="10" name="config[db_port]" size="40" type="text" value="3306" /></td>
         <td class="help">Optional. Default: 3306</td>
       </tr>
       <tr id="row-db-user">
         <td class="label"><label for="config_db_user">Database user</label></td>
         <td class="field"><input class="textbox" id="config_db_user" maxlength="255" name="config[db_user]" size="40" type="text" value="root" /></td>

         <td class="help">Required.</td>
       </tr>
       <tr id="row-db-pass">
         <td class="label"><label class="optional" for="config_db_pass">Database password</label></td>
         <td class="field"><input class="textbox" id="config_db_pass" maxlength="40" name="config[db_pass]" size="40" type="password" value="" /></td>
         <td class="help">Optional. If there is no database password, leave it blank.</td>
       </tr>
       <tr id="row-db-name">
         <td class="label"><label for="config_db_name">Database name</label></td>
         <td class="field"><input class="textbox" id="config_db_name" maxlength="40" name="config[db_name]" size="40" type="text" value="polliwog" /></td>
         <td class="help" id="help-db-name">Required. You have to create a database manually and enter its name here.</td>
       </tr>
       <tr id="row-table-prefix">
         <td class="label"><label class="optional" for="config_table_prefix">Table prefix</label></td>
         <td class="field"><input class="textbox" id="config_table_prefix" maxlength="40" name="config[table_prefix]" size="40" type="text" value="" /></td>
         <td class="help">Optional. Usefull to prevent conflicts if you have, or plan to have, multiple Frog installations with a single database.</td>
       </tr>
       <tr>
         <td colspan="3"><h3>Other information</h3></td>
       </tr>
       <tr>
         <td class="label"><label class="optional" for="config_admin_username">Administrator username</label></td>
         <td class="field"><input class="textbox" id="config_admin_username" maxlength="40" name="config[admin_username]" size="40" type="text" value="<?php echo DEFAULT_ADMIN_USER; ?>" /></td>
         <td class="help">Required. Allows you to specify a custom username for the administrator. Default: admin</td>
       </tr>
       <tr>
         <td class="label"><label class="optional" for="config_editor_username">Editor username</label></td>
         <td class="field"><input class="textbox" id="config_editor_username" maxlength="40" name="config[editor_username]" size="40" type="text" value="<?php echo DEFAULT_EDITOR_USER; ?>" /></td>
         <td class="help">Required. Allows you to specify a custom username for the editor. Default: editor</td>
       </tr>
       <tr>
         <td class="label"><label class="optional" for="config_url_suffix">URL suffix</label></td>
         <td class="field"><input class="textbox" id="config_url_suffix" maxlength="40" name="config[url_suffix]" size="40" type="text" value=".html" /></td>
         <td class="help">Optional. Add a suffix to simulate static html files.</td>
       </tr>
    </tbody>
  </table>

  <p class="buttons">
    <button class="button" name="commit" type="submit"> Install now </button>
  </p>

</form>

<?php else: ?>
    <?php echo $msg; ?>

    <?php if (isset($error)): ?>
    <p style="color: red;"><?php echo $error; ?></p>
    <p><a href="index.php">Click here and try again</a></p>
    <?php else: ?>
    <?php @chmod($config_file, 0444); ?>
        <p>
            <strong>Polliwog CMS</strong> is installed, <b>you should now:</b><br />
            1. delete the <em>install/</em> folder!<br />
            2. be sure that there are no write permissions on the <em>config.php</em> file!<br />
            3. remove changelog.txt and similar files to enhance security.
        </p>
    <?php endif; ?>

<?php endif; ?>

    </div>
  </div>
</div>

<div id="footer">
  <p>This system was made with <a href="http://www.php.net" target="_blank">PHP</a> and is powered by <a href="http://www.madebyfrog.com/">Frog CMS</a></p>
</div>
</body>
</html>
